*************************************************************************
*																		*
*					Campaign Panel Book Chapter 6 - Party Vote Models	*
*																		*
*************************************************************************	

use "${path}\Reproducible Do-Files\Chapter 6\Data\longformat13", clear

xtset lfdn welle


*---------------------------------
* Locals for IVs
*-----------------------------------

/* Independent of the party we are analysing vote intention for, the independent
variables are always the same ones (apart from solution competence). There are 
different blocks of variables: attitudes, communication and time. These independent
variables are concentrated in locals to make the code easier to understand 
as well as shorter */

local kommunikation awl tvduell1 tvduell4 tvduell_su1 tvduell_su4 tvduell_md1 tvduell_md4 wahlarena1 wahlarena4 pk_gesamtin? gespr? wahlomat1 wahlomat4 wahlomat5 wahlomat6 wahlomat7 syria_handling1 nsa_handling1 veggie6 prob_paed6 prob_mittelfinger4 maut1 glaubw_rrg4 bayern5
display "`kommunikation'"

local time w2 w3 w4 w5 w6 w7
display "`time'"

foreach partei of numlist 1 4/7 {
local attitudes`partei' koasge koagk koarg koarrg regzufr kand1 kand4  kand5 kand6 kand7 komp`partei'
display " `attitudes`partei''"
}

//list komp? // if komp is missing for a party, set to missing for all parties
			//variable rowmiss therefore does not have to be party dependent


* No missing on any variable
egen rowmiss = rowmiss(parta1 `kommunikation' `attitudes1')

list parta1 awl tvduell1 tvduell4 tvduell_su1 tvduell_su4 ///
	tvduell_md1 tvduell_md4 wahlarena1 wahlarena4 pk_gesamtin? gespr? ///
	wahlomat1 wahlomat4 wahlomat5 wahlomat6 wahlomat7 syria_handling1 ///
	nsa_handling1 veggie6 prob_paed6 prob_mittelfinger4 maut1 ///
	glaubw_rrg4 bayern5 koasge koagk koarg koarrg regzufr kand1 kand4 ///
	kand5 kand6 kand7 komp1 rowmiss in 1/10
	
drop if rowmiss~=0 //delete all observations containing missing values 
					//to achieve identical case numbers for all models

/* Allvalid was generated to indicate whether the DV has 7 valid measures.
But now we want to know whether there are 7 valid measures on DV and IVs.
*/


bysort lfdn: egen total = total(allvalid)
fre total  //Only if total = 7 do all waves possess valid measures on IV and DV

*Change working directory

cd "${path}\Reproducible Do-Files\Chapter 6"
	
*----------------------------------------------------
* Separate according to party identification
*----------------------------------------------------

xtlogit parta4 koasge koagk koarg koarrg regzufr kand1 kand4 ///
 kand5 kand6 kand7 komp4 w2 w3 w4 w5 w6 w7 if neverno==1, fe
//4.373 observations from 725 respondents


xtlogit parta4 koasge koagk koarg koarrg regzufr kand1 kand4 ///
 kand5 kand6 kand7 komp4 w2 w3 w4 w5 w6 w7 if total==7 & neverno==1, fe
//2.625 observations from 375 respondents

/* 
model 1 = only attitudes
model 2 = only communication
model 3 = attitudes + communication
*/

estimates clear

* Case selection A
foreach num of numlist 1 4/7  { 

xtlogit parta`num' `attitudes`num'' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model1_`num'						//store regression results

xtlogit parta`num' `kommunikation' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model2_`num'

xtlogit parta`num' `kommunikation' `attitudes`num'' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model3_`num'
gen model3_`num' = e(sample)										//store sample information in variable
estpost sum `kommunikation' `attitudes`num'' if e(sample)==1		//store means of independent variables
estimates store model3_`num'_uvs 

	foreach num2 of numlist 1 4/7 808 {
				
	xtlogit parta`num' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & pidfull==`num2', fe or iterate(10)
	estimates store model1_`num'_`num2'

	xtlogit parta`num' `kommunikation' `time' ///
		if neverno==1 & rowmiss==0 & pidfull==`num2', fe or iterate(10) 
	estimates store model2_`num'_`num2'

	
	xtlogit parta`num' `kommunikation' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & pidfull==`num2', fe or iterate(10) 
	estimates store model3_`num'_`num2'
	gen model3_`num'_`num2' = e(sample)
	estpost sum `kommunikation' `attitudes`num''  if e(sample)==1
	estimates store model3_`num'_`num2'_uvs 

	}
}


* Tabulate regression results
esttab model1_1 model1_1_1 model1_1_4 model1_1_5 model1_1_6 model1_1_7 model1_1_808 ///
	using tables_Model1_CDU_A13.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_4 model1_4_1 model1_4_4 model1_4_5 model1_4_6 model1_4_7 model1_4_808 ///
	using tables_Model1_SPD_A13.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_5 model1_5_1 model1_5_4 model1_5_5 model1_5_6 model1_5_7 model1_5_808 ///
	using tables_Model1_FDP_A13.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_6 model1_6_1 model1_6_4 model1_6_5 model1_6_6 model1_6_7 model1_6_808 ///
	using tables_Model1_GRU_A13.rtf, order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model1_7 model1_7_1 model1_7_4 model1_7_5 model1_7_6 model1_7_7 model1_7_808 using tables_Model1_LIN_A13.rtf, ///
	order(`attitudes`num'' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 

esttab model2_1 model2_1_1 model2_1_4 model2_1_5 model2_1_6 model2_1_7 model2_1_808 using tables_Model2_CDU_A13.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_4 model2_4_1 model2_4_4 model2_4_5 model2_4_6 model2_4_7 model2_4_808 using tables_Model2_SPD_A13.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_5 model2_5_1 model2_5_4 model2_5_5 model2_5_6 model2_5_7 model2_5_808 using tables_Model2_FDP_A13.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_6 model2_6_1 model2_6_4 model2_6_5 model2_6_6 model2_6_7 model2_6_808 using tables_Model2_GRU_A13.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model2_7 model2_7_1 model2_7_4 model2_7_5 model2_7_6 model2_7_7 model2_7_808 using tables_Model2_LIN_A13.rtf, ///
	order(`kommunikation' `time')  sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
	
	
esttab model3_1 model3_1_1 model3_1_4 model3_1_5 model3_1_6 model3_1_7 model3_1_808 using tables_model3_CDU_A13.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_4 model3_4_1 model3_4_4 model3_4_5 model3_4_6 model3_4_7 model3_4_808 using tables_model3_SPD_A13.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_5 model3_5_1 model3_5_4 model3_5_5 model3_5_6 model3_5_7 model3_5_808 using tables_model3_FDP_A13.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_6 model3_6_1 model3_6_4 model3_6_5 model3_6_6 model3_6_7 model3_6_808 using tables_model3_GRU_A13.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 
esttab model3_7 model3_7_1 model3_7_4 model3_7_5 model3_7_6 model3_7_7 model3_7_808 using tables_model3_LIN_A13.rtf, ///
	 sca(N_g r2_p) b(2) replace compress ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") 	

* Tabulate means of covariates	over all observations
esttab model3_1*uvs using descriptives_allmodels13.rtf,  cells(mean(fmt(2))) title("2013: Mittelwerte UVs Wahlabsicht für CDU/CSU") ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") replace compress
esttab model3_4*uvs using descriptives_allmodels13.rtf,  cells(mean(fmt(2)))  title("2013: Mittelwerte UVs Wahlabsicht für SPD") ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") append	compress
esttab model3_5*uvs using descriptives_allmodels13.rtf,  cells(mean(fmt(2)))  title("2013: Mittelwerte UVs Wahlabsicht für FDP") ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") append compress
esttab model3_6*uvs using descriptives_allmodels13.rtf,  cells(mean(fmt(2)))  title("2013: Mittelwerte UVs Wahlabsicht für Grüne") ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") append compress
esttab model3_7*uvs using descriptives_allmodels13.rtf,  cells(mean(fmt(2)))  title("2013: Mittelwerte UVs Wahlabsicht für Linke") ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") append compress
	
	

* Figure 8: figures development of vote intention for units of analysis of individual party vote models 	

foreach model of varlist model3_1 model3_4 model3_6  {
foreach num of numlist 0 1 4 5 6 7 {  
gen parta`num'_`model' = . 
foreach welle of numlist 1/7 {
sum parta`num' if welle==`welle' &  `model'==1
replace parta`num'_`model' = r(mean) if welle==`welle'
}
}
}

graph set window fontface "Times New Roman"



graph twoway connected parta1_model3_1 parta4_model3_1 parta5_model3_1 parta6_model3_1 parta7_model3_1 parta0_model3_1 welle, sort ///
	xlabel(1/7)  scheme(s1mono) ///
	title("", ///
	size(normal) margin(medium)) legend(label(1 CDU/CSU) label(2 SPD) label(3 FDP) label(4 Greens) label(5 "Left Party") label(6 "Undecided") ///
	region(lcolor(none)) cols(3)  position(6)) ///
	xtitle("Wave", margin(medium)) ytitle("Share of votes", margin(vsmall)) ///
	ylabel(0(0.1)0.7) name(cdu13, replace) ///
	lpattern(solid longdash_dot dash_dot dash vshortdash longdash) msymbol(O T + D S X) mcolor(gs6 gs4 gs12 gs10 gs8 black) 
graph export "Figures\CDU2013_3.tif", width(2500) ///
	height(1800) replace


graph twoway connected parta1_model3_4 parta4_model3_4 parta5_model3_4 parta6_model3_4 parta7_model3_4 parta0_model3_4 welle, sort ///
	xlabel(1/7)  scheme(s1mono) ///
	title("", ///
	size(normal) margin(medium)) legend(label(1 CDU/CSU) label(2 SPD) label(3 FDP) label(4 Greens) label(5 "Left Party") label(6 "Undecided") ///
	region(lcolor(none)) cols(3)  position(6)) ///
	xtitle("Wave", margin(medium)) ytitle("Share of votes", margin(vsmall)) ///
	ylabel(0(0.1)0.7) name(spd13, replace) ///
	lpattern(solid longdash_dot dash_dot dash vshortdash longdash) msymbol(O T + D S X) mcolor(gs6 gs4 gs12 gs10 gs8 black)
graph export "Figures\SPD2013_3.tif", width(2500) ///
	height(1800) replace

	
graph twoway connected parta1_model3_6 parta4_model3_6 parta5_model3_6 parta6_model3_6 parta7_model3_6 parta0_model3_6 welle, sort ///
	xlabel(1/7)  scheme(s1mono) ///
	title("", ///
	size(normal) margin(medium)) legend(label(1 CDU/CSU) label(2 SPD) label(3 FDP) label(4 Greens) label(5 "Left Party") label(6 "Undecided") ///
	region(lcolor(none)) cols(3) position(6)) ///
	xtitle("Wave", margin(medium)) ytitle("Share of votes", margin(vsmall)) ///
	ylabel(0(0.1)0.7) name(gru13, replace) ///
	lpattern(solid longdash_dot dash_dot dash vshortdash longdash) msymbol(O T + D S X) mcolor(gs6 gs4 gs12 gs10 gs8 black)
graph export "Figures\Greens2013_3.tif", width(2500) ///
	height(1800) replace

	
* How many Crystalizers, Waverers and Party Changers do the individual models contain?
foreach model of varlist model3* {
estpost tab laz if `model'==1 & welle==1, m
estimates store laz`model'
}

	
esttab lazmodel3_1* using "descriptives crystallizer in models13.rtf", ///
	cells("b pct(fmt(0))") compress replace  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2013: Anteil Kristallisierer, Waverer und Party Changer in CDU-Wahl-Modellen zu Welle 1")
esttab lazmodel3_4* using "descriptives crystallizer in models13.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2013: Anteil Kristallisierer, Waverer und Party Changer in SPD-Wahl-Modellen zu Welle 1")
esttab lazmodel3_5* using "descriptives crystallizer in models13.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2013: Anteil Kristallisierer, Waverer und Party Changer in FDP-Wahl-Modellen zu Welle 1")
esttab lazmodel3_6* using "descriptives crystallizer in models13.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2013: Anteil Kristallisierer, Waverer und Party Changer in Grünen-Wahl-Modellen zu Welle 1")
esttab lazmodel3_7* using "descriptives crystallizer in models13.rtf", ///
	cells("b pct(fmt(0))") compress append  varlabel(`e(labels)') ///
	mtitle("alle" "CDU-PID" "SPD-PID" "FDP-PID" "Green-PID" "Left-PID" "No PID") ///
	title("2013: Anteil Kristallisierer, Waverer und Party Changer in Linke-Wahl-Modellen zu Welle 1")




*-----------------------------------------------------------------------
* Combine individual party identification groups because of case number problems
*-------------------------------------------------------------------------


lab define ff ///
	1 "own party" ///
	2 "friend" ///
	3 "enemy" ///
	4 "Left ID" ///
	5 "No PID"
	
//CDU/CSU as DV
gen piddep1 = .
replace piddep1 = 1 if pidfull1==1 					//self
replace piddep1 = 2 if pidfull1==5				//friend
replace piddep1 = 3 if inlist(pidfull, 4, 6, 7) //foe
replace piddep1 = 5 if pidfull==808


//SPD as DV
gen piddep4 = .
replace piddep4 = 1 if pidfull1==4
replace piddep4 = 2 if pidfull1==6
replace piddep4 = 3 if inlist(pidfull, 1, 5)
replace piddep4 = 4 if pidfull1==7
replace piddep4 = 5 if pidfull==808

//FDP as DV
gen piddep5 = .
replace piddep5 = 1 if pidfull1==5
replace piddep5 = 2 if pidfull1==1
replace piddep5 = 3 if inlist(pidfull, 4, 6, 7)
replace piddep5 = 5 if pidfull==808


//Grüne as DV
gen piddep6 = .
replace piddep6 = 1 if pidfull1==6
replace piddep6 = 2 if pidfull1==4
replace piddep6 = 3 if inlist(pidfull, 1, 5)
replace piddep6 = 4 if pidfull1==7
replace piddep6 = 5 if pidfull==808

//Linke as DV
gen piddep7 = .
replace piddep7 = 1 if pidfull1==7  				//own
replace piddep7 = 2 if pidfull1==4 | pidfull1==6	//other Lefties
replace piddep7 = 3 if inlist(pidfull, 1, 5)		//righties
replace piddep7 = 5 if pidfull==808

//Labels
lab val piddep? ff

/* If the DV = CDU or FDP or Left, pidep? cannot be equal to four. Therefore
two loops have to be written for the calculation of the loops */


* Calculate models for CDU, FDP and Left vote intention
estimates clear
drop model*


foreach num of numlist 1 5 7  { 

xtlogit parta`num' `attitudes`num'' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model1_`num'						//store regression results


xtlogit parta`num' `kommunikation' `time' ///
	if neverno==1 & rowmiss==0, fe or iterate(10) 
estimates store model2_`num'


	foreach num2 of numlist 1 2 3 5 {
				
	xtlogit parta`num' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10)
	estimates store model1_`num'_`num2'


	xtlogit parta`num' `kommunikation' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10) 
	estimates store model2_`num'_`num2'

	}
}


* Calculate models for SPD and Green vote intention
foreach num of numlist 4 6 { 

xtlogit parta`num' `attitudes`num'' `time' if neverno==1, fe or iterate(10) 
estimates store model1_`num'						//store regression results


xtlogit parta`num' `kommunikation' `time' if neverno==1, fe or iterate(10) 
estimates store model2_`num'


	foreach num2 of numlist 1/5 {
				
	xtlogit parta`num' `attitudes`num'' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10)
	estimates store model1_`num'_`num2'


	xtlogit parta`num' `kommunikation' `time' ///
		if neverno==1 & rowmiss==0 & piddep`num'==`num2', fe or iterate(10) 
	estimates store model2_`num'_`num2'

	}
}



* Tabulate regression results
esttab model1_1 model1_1_? ///
	using table50.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not append compress ///
	mtitle("All" "CDU/CSU" "FDP" "Left Parties" "Indep.") nonumbers star(* 0.05)
	
esttab model1_4 model1_4_? ///
	using table51.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not append compress ///
	mtitle("All" "SPD" "Greens" "CDU/CSU FDP" "The Left" "Indep.") nonumbers star(* 0.05)
	
	
esttab model1_5 model1_5_? ///
	using table52.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not append compress ///
	mtitle("All" "FDP" "CDU/CSU" "Left Parties" "Indep.") nonumbers star(* 0.05)
	
esttab model1_6 model1_6_1 model1_6_2 model1_6_3 model1_6_5 ///
	using table53.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not append compress ///
	mtitle("All" "Greens" "SPD" "CDU/CSU FDP" "Indep.") nonumbers star(* 0.05)
	
esttab model1_7 model1_7_? ///
	using table54.rtf,  ///
	sca(N_g) b(2) drop(w?) order(kand? komp? regzufr koa*) not append compress ///
	mtitle("All" "The Left" "SPD Greens" "CDU/CSU FDP" "Indep.") nonumbers star(* 0.05) 
	
esttab model2_1 model2_4 model2_5 model2_6 model2_7 ///
	using table55.rtf, append compress ///
	sca(N_g) b(2) keep(tvduell* wahlarena*) order(tvduell* wahlarena*) mtitle("CDU/CSU" "SPD" "FDP" "Greens" "The Left") ///
	nonumbers star(* 0.05) not
	
esttab model2_1 model2_4 model2_5 model2_6 model2_7 ///
	using table56.rtf, append compress ///
	sca(N_g) b(2) keep(pk* gespr* wahlomat*) order(pk* gespr* wahlomat*) mtitle("CDU/CSU" "SPD" "FDP" "Greens" "The Left") ///
	nonumbers star(* 0.05) not
	
esttab model2_1 model2_4 model2_5 model2_6 model2_7 ///
	using table57.rtf, append compress ///
	sca(N_g) b(2) ///
	keep(awl nsa* syria* veggie* maut* prob_paed* prob_mitt* bayern5 glaubw*) ///
	order(awl nsa* syria* veggie* maut* prob_paed* prob_mitt* bayern5 glaubw*) ///
	mtitle("CDU/CSU" "SPD" "FDP" "Greens" "The Left") ///
	nonumbers star(* 0.05) not

estimates clear

estimates clear
